package cn.tiansu.eway.common.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

public class CmdUtils
{
	private final static Logger log = Logger.getLogger(CmdUtils.class);// 记录日志

	/**
	 * 运行shell脚本
	 * 
	 * @param shell
	 *            需要运行的shell脚本
	 */
	public static void execShell(String comand)
	{
		Process process = null;
		Reader inReader = null;
		List<String> processList = new ArrayList<String>();
		try
		{
			log.info("cmd = " + comand);
			String[] cmdStr =
			{ "bash", "-c", comand };
			process = Runtime.getRuntime().exec(cmdStr);
			BufferedReader input = new BufferedReader(new InputStreamReader(
					process.getInputStream()));
			String line = "";
			while ((line = input.readLine()) != null)
			{
				processList.add(line);
			}
			process.waitFor();

			for (String str : processList)
			{
				log.info(str);
			}

		} catch (Exception e)
		{
			log.error("", e);
		} finally
		{
			// if (process != null)
			// {
			// process.destroy();
			// process = null;
			// }
			if (inReader != null)
			{
				try
				{
					inReader.close();
				} catch (IOException e)
				{
					log.error(e);
					inReader = null;
				}
			}
		}
	}
}
